package com.egg.auth.rabbitmq.sender;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.egg.auth.constants.AuthRabbitExchange;
import com.egg.common.core.constant.rabbit.RabbitRoutingKey;
import com.egg.common.util.IdGenUtil;
import com.egg.logger.model.request.AccessPost;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;


/**
 * 发送
 *
 * @author east
 */
@Slf4j
@Async(value = "accessSenderThreadPoolTaskExecutor")
@Component
public class AccessSender {


    @Autowired(required = false)
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送-示例
     *
     * @param info
     */
    public void send(AccessPost info) {
        if (ObjectUtil.isEmpty(info)) {
            return;
        }
        String messageId = IdGenUtil.strId();
        String jsonInfo = JSON.toJSONString(info);
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setMessageId(messageId);
        Message message = new Message(jsonInfo.getBytes(), messageProperties);
        try {
            rabbitTemplate.convertAndSend(
                    AuthRabbitExchange.EXCHANGE_ACCESS_LOGIN
                    , RabbitRoutingKey.ADD
                    , message
            );
        } catch (Exception e) {
            log.debug("权限服务发送访问消息失败", e);
        }

    }

}
